package cn.yto.print.config;

import cn.hutool.json.JSONObject;
import cn.yto.print.config.i18n.I18nResourceManager;
import cn.yto.print.controller.IndexController;
import cn.yto.print.enums.MessageTypeEnum;
import cn.yto.print.enums.PluginRegistryEnum;
import cn.yto.print.system.SpringContextHolder;
import cn.yto.print.system.version.KhgjContactManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class PluginDownLoadConfig {

    private static final Map<String, String> PLUGIN_INSTALL_MAP = new HashMap<>();

    static {
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.CN.getCode(), "https://cdn-cloudprint.cainiao.com/waybill-print/cainiao-x-print/prod/win/cainiao-x-print-win-86.exe?spm=a219a.7386653.0.0.214c669aAlb85m&file=cainiao-x-print-win-86.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.PDD.getCode(), "https://meta.pinduoduo.com/api/one/app/v1/lateststable?appId=com.xunmeng.pddprint&platform=windows&subType=main");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.XHSX.getCode(), "https://xhswaybill-printer-1251524319.cos.ap-shanghai.myqcloud.com/XHSPrintClient/prod/windows/xhs-electron-printer_x64-setup.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.AKC.getCode(), "https://devdevnew-oss.aikucun.com/dayin/%E6%A2%A6%E9%A5%B7%E5%AE%98%E6%96%B9%E6%89%93%E5%8D%B0%E7%BB%84%E4%BB%B61.1.4.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.JD.getCode(), "https://prod-oms-app-cprt.jdwl.com/OpenCloudPrint/setup.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.DY.getCode(), "https://lf3-cm.ecombdstatic.com/obj/logistics-davinci/install/CloudPrintClient-1.0.4.9.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.KS.getCode(), "https://cloudprint.kwaixiaodian.com/rest/pc/scm/ebill/print/installer/download");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.WPH.getCode(), "https://vos.vipstatic.com/vip-lbs-printer/printer/vip_printer_setup_0.8.48.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.DW.getCode(), "https://h5static.dewu.com/print-app/client/win/%E5%BE%97%E7%89%A9%E6%89%93%E5%8D%B0%20Setup.exe");
        PLUGIN_INSTALL_MAP.put(PluginRegistryEnum.WXSPH.getCode(), "https://mmec-shop-1258344707.cos.ap-shanghai.myqcloud.com/shop/print/download/WeChatShopPrintClient-setup.exe");
    }

    public static String getPluginInstallUrl(String pluginName) {
        return PLUGIN_INSTALL_MAP.get(pluginName);
    }

    /**
     * 获取所有插件安装地址
     */
    public static Map<String, String> getPluginInstallMap() {
        return PLUGIN_INSTALL_MAP;
    }

    @PostConstruct
    public void initialize() {
        CompletableFuture.runAsync(() -> {
            JSONObject pluginDownloadUrls = KhgjContactManager.getPluginDownloadUrl();
            if (null != pluginDownloadUrls) {
                SpringContextHolder.getBean(IndexController.class).addConsoleLog(I18nResourceManager.getInstance().get("index.console.downUrl"), MessageTypeEnum.INFO);
                pluginDownloadUrls.keySet().forEach(key -> {
                    PLUGIN_INSTALL_MAP.put(key, pluginDownloadUrls.getStr(key));
                });
            }
        }, ThreadPoolManager.getExecutorService());
    }

}
